<?php

namespace Common\Model;

use Think\Model;

/**
 * 日志模型
 */
class LogModel extends Model
{

    const USER_SIGN = 0;
    const USER_CREATE_BBS = 1;           // 贡献日志类型：用户创建社区
    const USER_CREATE_PDOC = 2;   // 贡献日志类型：用户创建私有文档
    const USER_COLLECT_DOC = 3;
    const USER_FOCUS_BBS = 4;
    const USER_DONATE_USER = 5;
    const USER_DONATE_DOC = 6;
    const USER_DONATE_BBS = 7;

    const BBS_CREATE_DOC = -1;

    /**
     * 向日志表中添加数据
     * @param $data
     * @return mixed
     */
    public static function addData($data)
    {
        $data['create'] = time();
        return M('Log')->add($data);
    }

    /**
     * 获取团体的贡献日志记录
     * @param $bid
     * @param $p
     * @param $pcount
     * @return array
     */
    public static function getBbsLogHtml($bid, $p, $pcount)
    {
        $html = '';
        $count = M('Log')->where('sbid = '.$bid.' or gbid = '.$bid)->count();
        if ($count) {
            $list = M('Log')->where('sbid = '.$bid.' or gbid = '.$bid)->order('`create` desc')->page($p . ',' . $pcount)->select();
            $prevDateStr = '';
            $todayDateStr = date('Y-m-d', time());    // 今天的日期字符串
            $content = '';
            foreach ($list as $log) {
                $dateTmpStr = date('Y-m-d', $log['create']);
                if ($prevDateStr !== $dateTmpStr) {
                    if ($dateTmpStr === $todayDateStr) {
                        $headerStr = '<h3>Today.</h3>';
                    } else {
                        $headerStr = '<h3>' . $dateTmpStr . '</h3>';
                    }
                    $prevDateStr = $dateTmpStr;
                } else {
                    $headerStr = '';
                }
                switch ($log['type']) {
                    case self::USER_CREATE_BBS:
                        $content = '<li>用户「<a href="//wooo.wang/u/'.$log['suid'].'">'.UserModel::getUserName($log['suid']).'</a>」花费 '.$log['gx'].' 点贡献度创建本社区</li>';
                        break;
                    case self::USER_COLLECT_DOC:
                        $content = '<li>用户「<a href="//wooo.wang/u/'.$log['suid'].'">'.UserModel::getUserName($log['suid']).'</a>」花费 '.$log['gx'].' 点贡献度收藏文档「<a href="//doc.wooo.wang/show/'.$log['did'].'">'.DocModel::getDocName($log['did']).'</a>」</li>';
                        break;
                    case self::USER_FOCUS_BBS:
                        $content = '<li>用户「<a href="//wooo.wang/u/'.$log['suid'].'">'.UserModel::getUserName($log['suid']).'</a>」花费 '.$log['gx'].' 点贡献度关注本社区</li>';
                        break;
                    case self::USER_DONATE_DOC:
                        $content = '<li>用户「<a href="//wooo.wang/u/'.$log['suid'].'">'.UserModel::getUserName($log['suid']).'</a>」捐赠给文档 「<a href="//doc.wooo.wang/show/'.$log['did'].'">'.DocModel::getDocName($log['did']).'</a>」 '.$log['gx'].' 点贡献度</li>';
                        break;
                    case self::USER_DONATE_BBS:
                        $content = '<li>用户「<a href="//wooo.wang/u/'.$log['suid'].'">'.UserModel::getUserName($log['suid']).'</a>」捐赠给本社区 '.$log['gx'].' 点贡献度</li>';
                        break;
                    case self::BBS_CREATE_DOC:
                        $content = '<li>管委会主席「<a href="//wooo.wang/u/'.$log['suid'].'">'.UserModel::getUserName($log['suid']).'</a>」花费 '.$log['gx'].' 点贡献度创建文档「<a href="//doc.wooo.wang/show/'.$log['did'].'">'.DocModel::getDocName($log['did']).'</a>」</li>';
                        break;
                    default:
                        break;
                }
                $html .= $headerStr . $content;
            }
        }
        return [
            'count' => $count,
            'list' => $html
        ];
    }

    /**
     * 获取用户的贡献度日志记录
     * @param $uid
     * @param $p
     * @param $pcount
     * @return array
     */
    public static function getUserLogHtml($uid, $p, $pcount)
    {
        $html = '';
        $count = M('Log')->where('suid = '.$uid.' or guid = '.$uid.' and type >= 0')->count();
        if ($count) {
            $list = M('Log')->where('(suid = '.$uid.' or guid = '.$uid.') and type >= 0')->order('`create` desc')->page($p . ',' . $pcount)->select();
            $prevDateStr = '';
            $todayDateStr = date('Y-m-d', time());    // 今天的日期字符串
            $content = '';
            foreach ($list as $log) {
                $dateTmpStr = date('Y-m-d', $log['create']);
                if ($prevDateStr !== $dateTmpStr) {
                    if ($dateTmpStr === $todayDateStr) {
                        $headerStr = '<h3>Today.</h3>';
                    } else {
                        $headerStr = '<h3>' . $dateTmpStr . '</h3>';
                    }
                    $prevDateStr = $dateTmpStr;
                } else {
                    $headerStr = '';
                }
                switch ($log['type']) {
                    case self::USER_SIGN:
                        $content = '<li>签到增加 '.$log['gx'].' 点贡献度'.$log['note'].'</li>';
                        break;
                    case self::USER_CREATE_BBS:
                        $content = '<li>创建社区「<a href="//bbs.wooo.wang/'.$log['gbid'].'">'.BbsModel::getBbsName($log['gbid']).'</a>」花费 '.$log['gx'].' 点贡献度</li>';
                        break;
                    case self::USER_CREATE_PDOC:
                        $content = '<li>创建私有文档「<a href="//doc.wooo.wang/show/'.$log['did'].'">'.DocModel::getDocName($log['did']).'</a>」花费 '.$log['gx'].' 点贡献度</li>';
                        break;
                    case self::USER_COLLECT_DOC:
                        $content = '<li>收藏文档「<a href="//doc.wooo.wang/show/'.$log['did'].'">'.DocModel::getDocName($log['did']).'</a>」花费 '.$log['gx'].' 点贡献度</li>';
                        break;
                    case self::USER_FOCUS_BBS:
                        $content = '<li>关注社区「<a href="//bbs.wooo.wang/'.$log['gbid'].'">'.BbsModel::getBbsName($log['gbid']).'</a>」花费 '.$log['gx'].' 点贡献度</li>';
                        break;
                    case self::USER_DONATE_USER:
                        if ($log['suid'] == $uid) {
                            $content = '<li>打赏用户「<a href="//wooo.wang/u/'.$log['guid'].'">'.UserModel::getUserName($log['guid']).'</a>」 '.$log['gx'].' 点贡献度</li>';
                        } else {
                            $content = '<li>用户「<a href="//wooo.wang/u/'.$log['suid'].'">'.UserModel::getUserName($log['suid']).'</a>」 赠送给您 '.$log['gx'].' 点贡献度</li>';
                        }
                        break;
                    case self::USER_DONATE_DOC:
                        $content = '<li>捐赠文档「<a href="//doc.wooo.wang/show/'.$log['did'].'">'.DocModel::getDocName($log['did']).'</a>」 '.$log['gx'].' 点贡献度</li>';
                        break;
                    case self::USER_DONATE_BBS:
                        $content = '<li>捐赠社区「<a href="//bbs.wooo.wang/'.$log['gbid'].'">'.BbsModel::getBbsName($log['gbid']).'</a>」 '.$log['gx'].' 点贡献度</li>';
                        break;
                    default:
                        break;
                }
                $html .= $headerStr . $content;
            }
        }
        return [
            'count' => $count,
            'list' => $html
        ];
    }

    /**
     * 用户签到
     * @param $uid
     * @param $signs
     * @param $gx
     * @return mixed
     */
    public static function userSign($uid, $signs, $gx)
    {
        $data = [
            'suid' => $uid,
            'gx' => $gx,
            'type' => self::USER_SIGN,
            'note' => '（连续签到 ' . $signs . ' 天）'
        ];
        return self::addData($data);
    }

    /**
     * 用户创建社区
     * @param $uid
     * @param $gx
     * @param $bid
     * @return mixed
     */
    public static function userCreateBbs($uid, $gx, $bid)
    {
        $data = [
            'suid' => $uid,
            'gx' => $gx,
            'gbid' => $bid,
            'type' => self::USER_CREATE_BBS
        ];
        return self::addData($data);
    }

    /**
     * 用户创建私有文档
     * @param $uid
     * @param $gx
     * @param $did
     * @return mixed
     */
    public static function userCreatePDoc($uid, $gx, $did)
    {
        $data = [
            'suid' => $uid,
            'gx' => $gx,
            'did' => $did,
            'type' => self::USER_CREATE_PDOC
        ];
        return self::addData($data);
    }

    /**
     * 用户收藏文档
     * @param $uid
     * @param $doc
     * @return mixed
     */
    public static function userCollectDoc($uid, $doc)
    {
        $data = [
            'suid' => $uid,
            'did' => $doc['id'],
            'gx' => 1,
            'type' => self::USER_COLLECT_DOC
        ];
        if ($doc['bid']) {
            $data['gbid'] = $doc['bid'];
        } else {
            $data['guid'] = $doc['uid'];
        }
        return self::addData($data);
    }

    /**
     * 用户关注社区
     * @param $uid
     * @param $bid
     * @return mixed
     */
    public static function userFocusBbs($uid, $bid)
    {
        $data = [
            'suid' => $uid,
            'gbid' => $bid,
            'gx' => 1,
            'type' => self::USER_FOCUS_BBS
        ];
        return self::addData($data);
    }

    /**
     * 用户捐赠用户
     * @param $uid
     * @param $gx
     * @param $guid
     * @return mixed
     */
    public static function userDonateUser($uid, $gx, $guid)
    {
        $data = [
            'suid' => $uid,
            'gx' => $gx,
            'guid' => $guid,
            'type' => self::USER_DONATE_USER
        ];
        return self::addData($data);
    }

    /**
     * 用户捐赠文档
     * @param $uid
     * @param $gx
     * @param $doc
     * @return mixed
     */
    public static function userDonateDoc($uid, $gx, $doc)
    {
        $data = [
            'suid' => $uid,
            'gx' => $gx,
            'did' => $doc['id'],
            'type' => self::USER_DONATE_DOC
        ];
        if ($doc['bid']) {
            $data['gbid'] = $doc['bid'];
        } else {
            $data['guid'] = $doc['uid'];
        }
        return self::addData($data);
    }

    /**
     * 用户捐赠社区
     * @param $uid
     * @param $gx
     * @param $bid
     * @return mixed
     */
    public static function userDonateBbs($uid, $gx, $bid)
    {
        $data = [
            'suid' => $uid,
            'gx' => $gx,
            'gbid' => $bid,
            'type' => self::USER_DONATE_BBS
        ];
        return self::addData($data);
    }

    /**
     * 社区创建文档
     * @param $bid
     * @param $uid
     * @param $gx
     * @param $did
     * @return mixed
     */
    public static function bbsCreateDoc($bid, $uid, $gx, $did)
    {
        $data = [
            'suid' => $uid,
            'sbid' => $bid,
            'gx' => $gx,
            'did' => $did,
            'type' => self::BBS_CREATE_DOC
        ];
        return self::addData($data);
    }

}